<?php

/**
 * This is the model class for table "{{blocks}}".
 *
 * The followings are the available columns in table '{{blocks}}':
 * @property integer $id
 * @property string $name
 * @property integer $show_header
 * @property string $anons
 * @property string $text
 * @property integer $type_id
 * @property string $video_url
 * @property string $crdate
 * @property string $file
 * @property string $url
 */
class Blocks extends BaseModel
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Blocks the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{blocks}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, show_header, type_id', 'required'),
			array('show_header, type_id', 'numerical', 'integerOnly'=>true),
			array('name, file', 'length', 'max'=>1000),
			array('video_url, url', 'length', 'max'=>255),
			array('anons, text', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, show_header, anons, text, type_id, video_url, crdate, file, url', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'type' => array(self::BELONGS_TO, 'BlocksType', 'type_id'),
			'lns' => array(self::HAS_MANY, 'BlocksLn', 'block_id'),
			'ln' => array(self::HAS_ONE, 'BlocksLn', 'block_id',
					'condition'=>'ln.ln=:ln',
					'params'=>array(':ln'=>Yii::app()->language),
			),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' 			=>	Yii::t('main', 'ID'),
			'name' 			=>	Yii::t('main', 'Название'),
			'show_header'	=>	Yii::t('main', 'Показывать заголовок'),
			'anons' 		=>	Yii::t('main', 'Текст'),
			'text'			=>	Yii::t('main', 'HTML'),
			'type_id' 		=>	Yii::t('main', 'Тип блока'),
			'video_url' 	=>	Yii::t('main', 'Ссылка на видео'),
			'crdate' 		=>	Yii::t('main', 'Дата создания'),
			'file' 			=>	Yii::t('main', 'Файл'),
			'url' 			=>	Yii::t('main', 'Ссылка'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('show_header',$this->show_header);
		$criteria->compare('anons',$this->anons,true);
		$criteria->compare('text',$this->text,true);
		$criteria->compare('type_id',$this->type_id);
		$criteria->compare('video_url',$this->video_url,true);
		$criteria->compare('crdate',$this->crdate,true);
		$criteria->compare('file',$this->file,true);
		$criteria->compare('url',$this->url,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}